// 每组任务, 配置是同步的, 所有配置划掉的时间是确定的, 就是不知道这个时候有哪些任务已经完成, 所以, 运行时间, 应该倒着来, 耗时短的在后面执行,
// 配置时间+ 最后一项任务完成的时间, 与其他时间进行对比, 取最大值
const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let groupLength;
let group = [];
let tempN = 0

rl.on("line", (line)=>{
    if(groupLength === undefined) {
        groupLength = Number(line.trim())
    } else {
        if(tempN === 0) {
            tempN = Number(line.trim())
            group.push({
                n: tempN,
                initArr: []
            })
        } else {
            let arr = line.trim().split(" ").map(Number)
            group.at(-1).initArr.push(arr)
            tempN--
            if(tempN === 0 && group.length === groupLength) {
                getResult(group)
                // clear
                groupLength = undefined
                group.length = 0
                tempN = 0
            }
        }
    }

})

function getResult(group) {
    for (const obj of group) {
        let arr = obj.initArr
        // end 降序
        arr.sort((a, b) => b[1] - a[1])
        let config = 0
        let runMax = 0
        for (const [a, b] of arr) {
            config += a
            runMax = Math.max(runMax, config + b)
        }
        console.log(runMax)
    }
}